<template>
  <div>
    <n-spin :show="show" description="请稍候...">
      <n-form :label-width="80" :model="formValue" :rules="rules" ref="formRef">
        <n-form-item label="允许使用的支付方式" path="appversion">
          <n-checkbox-group v-model:value="payMode" name="radioGroup">
            <n-checkbox value="WeChatPay">WeChatPay</n-checkbox>
            <n-checkbox value="Alipay+">Alipay+</n-checkbox>
            <n-checkbox value="Paypal">Paypal</n-checkbox>
            <n-checkbox value="PaypalCard">PaypalCard</n-checkbox>
            <n-checkbox value="StripeCard">StripeCard</n-checkbox>
          </n-checkbox-group>
        </n-form-item>

        <div>
          <n-space>
            <n-button type="primary" @click="formSubmit">保存更新</n-button>
          </n-space>
        </div>
      </n-form>
    </n-spin>
  </div>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { useDialog, useMessage } from 'naive-ui';
  import { getConfig, updateConfig } from '@/api/sys/config';

  const group = ref('paymodeconfig');
  const show = ref(false);
  const formRef: any = ref(null);
  const message = useMessage();
  const dialog = useDialog();

  const formValue = ref({
    paymode: '',

  });

  const payMode = ref([])

  const rules = {
  /*  basicName: {
      required: true,
      message: '请输入网站名称',
      trigger: 'blur',
    },*/
  };

  function formSubmit() {
    formRef.value.validate((errors) => {
      if (!errors) {
        formValue.value.paymode = payMode.value.join(",")
        updateConfig({ group: group.value, list: formValue.value }).then((_res) => {
          message.success('更新成功');
          load();
        });
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

  onMounted(() => {
    load();
  });

  function load() {
    show.value = true;
    new Promise((_resolve, _reject) => {
      getConfig({ group: group.value })
        .then((res) => {
          payMode.value = res.list.paymode.split(",").map(item => String(item));
          formValue.value = res.list;
        })
        .finally(() => {
          show.value = false;
        });
    });
  }
</script>
